var app = getApp();
Page({
  onLoad(){
    app.judgelogin();
    let lists=wx.getStorageSync('bookorder');
    this.setData({ lists: lists});
    let bookids = new Array();
    for (let i = 0; i < lists.length; i++) {
      bookids.push(lists[i].book_id)
    }
    let date = wx.getStorageSync('date');//选中的日期
    let _jydate = date.split('-');//借阅开始时间
    let enddate = this.endtime(date, 30, '1');//借阅结束时间yyyy-mm-dd
    let jydate = _jydate[1] + '月' + _jydate[2] + '日';
    let jymaxdate = this.endtime(date,30,'2');//借阅结束时间 几月几日
    this.setData({ jydate: jydate, jymaxdate: jymaxdate, bookids: bookids, date: date, enddate: enddate});
    this.getmycards();
    wx.showLoading({ title: '加载中...'})
    this.oneaddress('','1');
    
  },
  data:{
    addressid:0,//收货地址id
    address:{},
    yunfei:0,//运费
    zujin:0,//租金
    yajin:0,
    cardid:0,//会员卡号
    havecard:false,//有无会员卡
    youhuiprice:0,//优惠价格
    haveyouhui:false,//有没有优惠卷
    hadyouhui:false,//选没选优惠卷
    coupon_no:'',//优惠券编号
    message:'',//留言
    jianyajin:"",
   // chazujinyajin:false //是否查询过直购租金押金
  },
  oneaddress(id = '', moren = '0') {//收获地址
    if(id==-1){
      this.setData({ address: {}, addressid: 0});
      return false
    }
    let token = wx.getStorageSync('token');
    let user_id = wx.getStorageSync('userdata').user_id;
    let json = { token: token, userId: user_id, id: id, is_default: moren }
    app.request('index/User/userAddressInfo', 'POST', json, 'oneaddressb', this);
  },
  oneaddressb(data) {//地址回调
   // console.log(data);
    if(data.code!=200){//没有默认地址找一下上次用的地址
      let token = wx.getStorageSync('token');
      let user_id = wx.getStorageSync('userdata').user_id;
      let json = { token: token, user_id: user_id};
      app.request('book/card/defaultAddress', 'POST', json,'oneaddressb',this);
      return false;
    }   
    if (data.data.default_address){//没有默认地址，上次购买用的地址
      this.setData({ address: data.data.default_address, 
      addressid: data.data.default_address.address_id});
      return false;
    }
    if (data.data.service_area){return false}//300,走的上次购买地址回调
    this.setData({ address: data.data.addressList, addressid: data.data.addressList.id});
  },
  checkaddress(e){//跳到地址列表
    if (app.repeat(e.timeStamp) == false) { return; }
    let id=e.currentTarget.id;
    wx.navigateTo({ url: '/pages/home/addresslist/addresslist?id='+id })
  },
  zhima() {//芝麻信用
    let user_id = wx.getStorageSync('userdata').user_id;
    app.request('index/zhima/getUserZMScore', 'POST', { user_id: user_id }, 'zhimab', this)
  },
  zhimab(data) {//芝麻信用
    wx.hideLoading();
    if (data.data.status == 0 || data.data.status == 2) {
      this.setData({ jianyajin: "0" }) 
      return false
    }
    if (data.data.zmscore >= 600) {
      this.setData({ jianyajin: "1", yajin: 0 })
    }
   // this.setData({chazujinyajin:true});
  },
  getmycards(){//会员卡
    let token=wx.getStorageSync('token');
    let user_id=wx.getStorageSync('userdata').user_id;
    let book_num=this.data.lists.length;
    let json={
      token: token,
      user_id: user_id,
      book_num: book_num,
      version: '2.0.1'
    }
    app.request('book/user/myValidCards', 'GET', json,'getmycardsb',this);
  },
  getmycardsb(data){//获取会员卡
    wx.hideLoading();
    if(data.code!=200){      
      app.tishi('提示',data.msg);
      return false;
    }
    let cards = data.data.cards;
    let book_num = this.data.lists.length;
    if (!data.data.cards_num || (data.data.cards_num&&data.data.cards_num == 0)){//没有可用计划||没卡
      this.setData({ jihuashu: 0 });
      if (cards&&cards.length > 0) {//有卡，都不可用
        this.setData({ jihuashu: 1, cardname: cards[0].name + "(暂不可用)" });
      }
      this.zhigouzujin();//获取直接购买租金
      this.youhuijuan();//获取优惠卷
      this.zhima();//获取芝麻信用
      return false;
    }
    else if (data.data.cards_num == 1){//只有一张可用
        this.setData({ jihuashu: 1, cardname: cards[0].name, cardid: cards[0].user_card_id, havecard: true });       
    }  
    else if (data.data.cards_num > 1){//多张可用会员卡
      if (book_num > 10){//大于10本，勾选上可用计划
        this.setData({ jihuashu: 1, cardname: cards[0].name, cardid: cards[0].user_card_id, havecard: true });
        return false;
      }
      this.setData({ jihuashu: 1, cardname: data.data.cards_num+'张可选',  havecard: false});
      this.zhigouzujin();//获取直接购买租金
      this.youhuijuan();//获取优惠卷
      this.zhima();//获取芝麻信用
    }
  },
  chosecard(e){//多种计划，去选择
    if (app.repeat(e.timeStamp) == false) { return; }
    wx.navigateTo({url: '/pages/home/mycard/mycard?from=orderinfo' })
  },
  chosecardb(cardid,name){//回调，会员卡号赋值
    if (cardid==0){//不使用会员计划
      this.setData({ cardname: name, cardid: 0, havecard: false});
     // if (this.data.chazujinyajin==false){//之前没查过就查一下租金押金
        wx.showLoading();
        this.zhigouzujin();//获取直接购买租金
        this.youhuijuan();//获取优惠卷
        this.zhima();//获取芝麻信用
      //}
    }else{
      this.setData({ cardname: name, cardid: cardid, havecard: true,zujin:0});
    }
  },
  zhigouzujin(){//获取租金
    let json = { books: this.data.bookids};
     app.request('book/order/getPurchasePackage', 'POST', json,'zhigouzujinb',this);
  },
  zhigouzujinb(data){//租金押金
    wx.hideLoading();
    if (data.code != 200) {
      setTimeout(()=>{
        app.tishi('提示', data.msg)
      },600)    
      return false;
    }
    this.setData({ zujin: data.data.rent_price, yajin: data.data.deposit, yunfei: data.data.freight});
  },
  youhuijuan(){//查优惠卷
    let token = wx.getStorageSync('token');
    let user_id = wx.getStorageSync('userdata').user_id;
    let goods_type="1";
    let json = { token: token, userId: user_id, goods_type: goods_type,"type":"1"}
    app.request('index/coupon/userCoupon','POST',json,'youhuijuanb',this);
  },
  youhuijuanb(data){//优惠查询回调
  //  this.setData({ chazujinyajin: true })
//    console.log(data);
    wx.hideLoading();
    if(data.code!=200){
      return false;
    }
    if(data.data.length==0){
      this.setData({ youhuitext:'暂无可用红包'})
    } else if (data.data.length == 1){//一张默认选中
//    console.log(data.data)
      this.setData({
        youhuiprice: parseInt(data.data[0].amount), youhuitext: data.data[0].coupon_name, 
        coupon_no: data.data[0].coupon_no, haveyouhui: true, hadyouhui:true})
    }
    else if (data.data.length > 1){   
      this.setData({ youhuitext: data.data.length + '张可用', haveyouhui:true})
    }
  },
  checkyouhui(e){//选择优惠卷
    if (this.data.haveyouhui){
      wx.navigateTo({ url: '/pages/home/coupontype/coupontype?type=1',})
    }
  },
  useyouhui(e){//使用优惠卷,在优惠列表里选择了
    if(e=='no'){
      this.setData({
        youhuiprice: 0, youhuitext: '不使用优惠',
        coupon_no: '', hadyouhui: false,
      })
      return false;
    }
    this.setData({
      youhuiprice: parseInt(e.amount), youhuitext: e.coupon_name, 
    coupon_no: e.coupon_no, hadyouhui:true})
  },
  submit(e){
    if (app.repeat(e.timeStamp) == false) { return; }
    if (this.data.addressid==0){//没地址
      app.tishi('提示','请选择收货地址');
      return false;
    } 
    if (this.data.cardid==0){//直购
      this.zhigou();
    }else{//会员卡购买
      // if(this.data.cardid==0){
      //   app.tishi('提示','请选择会员计划');
      //   return false;
      // }
      let _this=this;
      wx.showModal({
        title: '提示',
        content: '会员计划无需付款，订单立即生效',
        cancelText: '再选选',
        confirmText: '选好啦',
        success(res){
          if(res.confirm){
            _this.cardbuy();
          }
        }
      })  
    }
  },
  cardbuy(){
    wx.showLoading({ title: '调起支付...', mask: true });
    let token = wx.getStorageSync('token');
    let user_id = wx.getStorageSync('userdata').user_id;
    let address_id = this.data.addressid;
    let books = this.data.bookids;
    let start_date = this.data.date;
    let end_date = this.data.enddate;
    let message = this.data.message;
    let card_id=this.data.cardid;
    let json = {
      token: token, user_id: user_id, address_id: address_id, books: books, start_date: start_date,
      end_date: end_date, message: message, card_id: card_id}
//    console.log(json);
    app.request('book/order/createOrder','POST',json,'cardbuyb',this);
  },
  cardbuyb(data){
//      console.log(data);
      if(data.code!=200){
        app.tishi('提示',data.msg)
        wx.hideLoading();
        return false
      }else{
        wx.showToast({title: '购买成功',icon:'success',duration:1000});
        setTimeout(()=>{
          wx.redirectTo({ url: '/pages/order/order?id=2' })
        },1000)
      }
  },
  zhigou(){//直购
    wx.showLoading({title: '调起支付...',mask:true})
    let token=wx.getStorageSync('token');
    let user_id=wx.getStorageSync('userdata').user_id;
    let address_id = this.data.addressid;
    let rent_price = this.data.zujin;
    let coupon_no = this.data.coupon_no;
    let freight = 0.00;
    let books = this.data.bookids;
    let start_date =this.data.date;
    let end_date = this.data.enddate;
    let message = this.data.message;
    let json = { token: token, user_id: user_id, address_id: address_id, rent_price: rent_price,
      coupon_no: coupon_no, freight: freight, books: books, start_date: start_date,
      end_date: end_date, message: message}
    app.request('book/order/createBookOnceOrder','POST',json,'zhigoub',this);
  },
  zhigoub(data){
    if(data.code!=200){
      wx.hideLoading();
      app.tishi('提示',data.msg)
      return false;
    }
    let token = wx.getStorageSync('token');
    let userId = wx.getStorageSync('userdata').user_id;
    let orderSn = data.data.order_sn;
    let openId=wx.getStorageSync('openid');
    let json={
      token: token, userId: userId, orderSn: orderSn, payMethod: 5, "type": 1, openId: openId
    }
    app.payrequest('pay/jsapi/cardPay','POST',json,'zhigoupay',this);
  },
  zhigoupay(data){//直购支付
    if (data.code && data.code == 300) {
      wx.showToast({ title: data.msg, mask: true, duration: 1000, icon: 'none' });
      return false
    }
    if (data.code && data.code == 250) {
      wx.hideLoading();
      wx.showToast({ title: '购买成功！', icon: 'success', mask: true, duration: 1000 });
      setTimeout(() => {
        wx.redirectTo({ url: '/pages/order/order?id=2' })
      }, 1000)
      return false
    }
//    console.log(data);
    wx.requestPayment({
      'timeStamp': data.timeStamp,
      'nonceStr': data.nonceStr,
      'package': data.package,
      'signType': data.signType,
      'paySign': data.paySign,
      success(e){
        wx.hideLoading();
        wx.showToast({title: '购买成功！',icon:'success',mask:true,duration:1000});
        setTimeout(()=>{
          wx.redirectTo({ url: '/pages/order/order?id=2' })
        },1000)
      },
      fail(e){
        wx.hideLoading();
        wx.showToast({ title: '支付取消',mask: true, icon:'none',duration: 500 });
        setTimeout(() => {
          wx.redirectTo({url: '/pages/order/order?id=1'})
        }, 500)
      }
    })
    let token = wx.getStorageSync('token');
    let user_id = wx.getStorageSync('userdata').user_id;
    app.request('book/user/bookShelfNum', 'POST', { token: token, user_id: user_id }, 'shelfnum', app);
  },
  endtime(e,days,_type){
    let date = new Date(e);
    date.setDate(date.getDate() + days);
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    if (month < 10) { month = "0" + month };
    if (day < 10) { day = "0" + day };
    if (_type=='1'){
      return year + '-' + month + '-' + day;
    }else{
      return  month + '月' + day+'日';
    }   
  },
  message(e){
    this.setData({ message:e.detail.value});
  },
  tofixed(e){//两位小数
   return parseFloat(e).toFixed(2);
  },
  gohuiyuan(){//去购买会员
    wx.redirectTo({url: '/pages/home/buycard/buycard'})
  },
  gozhima(){//去芝麻信用页
    wx.redirectTo({url: '/pages/home/zhima/zhima'})
  },
  gozhanshi(e){
    if (app.repeat(e.timeStamp) == false) { return; }
    wx.navigateTo({url: '/pages/shelf/zhanshi/zhanshi'})
  }
})